﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Text;
using Dapper;
using System.Collections.Generic;

namespace AyaWo.DbF
{
    /// <summary>
    /// SqLite数据库连接方法
    /// </summary>
    public class DbHelper : IDbHelper, IDisposable
    {
        public DbHelper(string connection, string dbType)
        {
            ConnectionString = connection;
            if (dbType == "Oracle")
                DbType = SqlSourceType.Oracle;
            if (dbType == "SqlServer")
                DbType = SqlSourceType.SqlServer;
            else if (dbType == "MySql")
                DbType = SqlSourceType.MySql;
            else if (dbType == "Access")
                DbType = SqlSourceType.Access;
            else if (dbType == "SqLite")
                DbType = SqlSourceType.SqLite;
            else if (dbType == "Db2")
                DbType = SqlSourceType.Db2;
            else if (dbType == "PostgreSql")
                DbType = SqlSourceType.PostgreSql;
            else
                DbType = SqlSourceType.SqLite;
        }
        /// <summary>
        /// SqLiteHelper构造函数
        /// </summary>
        /// <param name="connection"></param>
        public DbHelper(string connection, SqlSourceType dbType)
        {
            ConnectionString = connection;
            DbType = dbType;
        }

        #region 数据库连接必要条件参数

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        protected string ConnectionString = "";

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        protected SqlSourceType DbType;

        /// <summary>
        /// 数据库连接
        /// </summary>
        /// <returns></returns>
        public IDbConnection OpenConnection()
        {
            IDbConnection connection = null;
            switch (DbType)
            {
                case SqlSourceType.SqlServer:
                    connection = new System.Data.SqlClient.SqlConnection(ConnectionString);
                    connection.Open();
                    break;
                case SqlSourceType.SqLite:
                    connection = new System.Data.SQLite.SQLiteConnection(ConnectionString);
                    connection.Open();
                    break;
                case SqlSourceType.MySql:
                    connection = new MySql.Data.MySqlClient.MySqlConnection(ConnectionString);
                    connection.Open();
                    break;
                    /* case SqlSourceType.Oracle:
                         connection = new Oracle.DataAccess.Client.OracleConnection(ConnectionString);
                         connection.Open();
                         break;*/
                    //case SqlSourceType.Db2:
                    //    connection = new System.Data.OleDb.OleDbConnection(ConnectionString);
                    //    connection.Open();
                    //    break;
            }
            return connection;
        }

        /// <summary>
        /// 事物
        /// </summary>
        private IDbTransaction _dbTransaction = null;

        #endregion

        #region Dapper操作方法

        public void SetDialect()
        {
            switch (DbType)
            {
                case SqlSourceType.SqLite:
                    SimpleCRUD.SetDialect(SimpleCRUD.Dialect.SQLite);
                    break;
                case SqlSourceType.MySql:
                    SimpleCRUD.SetDialect(SimpleCRUD.Dialect.MySQL);
                    break;
                case SqlSourceType.PostgreSql:
                    SimpleCRUD.SetDialect(SimpleCRUD.Dialect.PostgreSQL);
                    break;
                case SqlSourceType.SqlServer:
                    SimpleCRUD.SetDialect(SimpleCRUD.Dialect.SQLServer);
                    break;
            }

        }



        /// <summary>
        /// 根据主键获取单一对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public T Get<T>(object id)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    return conn.Get<T>(id);
                }
                catch (Exception e)
                {

                    DbLog.WriteException(e);
                    return default(T);
                }

            }
        }

        public T GetByWhere<T>(string strWhere)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    return conn.GetByWhere<T>(strWhere);
                }
                catch (Exception e)
                {

                    DbLog.WriteException(e);
                    return default(T);
                }

            }
        }


        /// <summary>
        /// 获取实体对象列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parm">参数</param>
        /// <returns></returns>
        public IEnumerable<T> GetList<T>(object parm)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    return conn.GetList<T>(parm);
                }
                catch (Exception e)
                {

                    DbLog.WriteException(e);
                    return null;
                }

            }
        }

        /// <summary>
        /// 获取实体对象列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="strwhere">Where条件</param>
        /// <returns></returns>
        public IEnumerable<T> GetList<T>(string strwhere)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    return conn.GetList<T>(strwhere);
                }
                catch (Exception e)
                {

                    DbLog.WriteException(e);
                    return null;
                }

            }
        }

        /// <summary>
        /// 实体对象列表分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageNumber">查询第几页</param>
        /// <param name="rowsPerPage">每页显示的条数</param>
        /// <param name="strWhere">Where条件</param>
        /// <param name="orderby">排序字段</param>
        /// <param name="count">数据总页数</param>
        /// <returns></returns>
        public IEnumerable<T> GetListPage<T>(int pageNumber, int rowsPerPage, string strWhere, string orderby, ref int count)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    count = conn.RecordCount<T>(strWhere);
                    return conn.GetListPaged<T>(pageNumber, rowsPerPage, strWhere, orderby);
                }
                catch (Exception e)
                {

                    DbLog.WriteException(e);
                    return null;
                }

            }
        }

        /// <summary>
        /// 根据实体对象插入数据
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回Guid类型</returns>
        public Guid InsertGuid(object entity)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    return conn.Insert<Guid>(entity);
                }
                catch (Exception e)
                {
                    DbLog.WriteException(e);
                    return Guid.Empty;
                }
            }
        }

        /// <summary>
        /// 带事务的实体对象插入数据
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回Guid类型</returns>
        public Guid InsertTranGuid(object entity)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    _dbTransaction = conn.BeginTransaction();
                    var num = conn.Insert<Guid>(entity, _dbTransaction);
                    _dbTransaction.Commit();
                    return num;
                }
                catch (Exception e)
                {
                    _dbTransaction.Rollback();
                    DbLog.WriteException(e);
                    return Guid.Empty;
                }
                finally
                {
                    conn.Close();
                    conn.Dispose();
                    _dbTransaction.Dispose();
                }
            }
        }

        /// <summary>
        /// 批量插入数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="entities"></param>
        /// <returns></returns>
        public int InsertMultiple<T>(string sql, IEnumerable<T> entities)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    _dbTransaction = conn.BeginTransaction();
                    conn.Execute(sql, entities, _dbTransaction, 30, CommandType.Text);
                    _dbTransaction.Commit();
                    return 1;
                }
                catch (Exception e)
                {
                    _dbTransaction.Rollback();
                    DbLog.WriteException(e);
                    return 0;
                }
                finally
                {
                    conn.Close();
                    conn.Dispose();
                    _dbTransaction.Dispose();
                }
            }
        }

        /// <summary>
        /// 根据实体对象插入数据
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回Int类型</returns>
        public int? Insert(object entity)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    return conn.Insert(entity);

                }
                catch (Exception e)
                {
                    DbLog.WriteException(e);
                    return 0;
                }
            }
        }

        /// <summary>
        /// 带事务的实体对象插入数据
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public int? InsertTran(object entity)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    _dbTransaction = conn.BeginTransaction();
                    var num = conn.Insert(entity, _dbTransaction);
                    _dbTransaction.Commit();
                    return num;
                }
                catch (Exception e)
                {
                    _dbTransaction.Rollback();
                    DbLog.WriteException(e);
                    return 0;
                }
                finally
                {
                    conn.Close();
                    conn.Dispose();
                    _dbTransaction.Dispose();
                }
            }
        }

        /// <summary>
        /// 根据实体对象更新数据
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public int? Update(object entity)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    return conn.Update(entity);
                }
                catch (Exception e)
                {
                    DbLog.WriteException(e);
                    return 0;
                }
            }
        }

        /// <summary>
        /// 带事务的实体对象更新数据
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public int? UpdateTran(object entity)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    _dbTransaction = conn.BeginTransaction();
                    var num = conn.Update(entity, _dbTransaction);
                    _dbTransaction.Commit();
                    return num;
                }
                catch (Exception e)
                {
                    _dbTransaction.Rollback();
                    DbLog.WriteException(e);
                    return 0;
                }
                finally
                {
                    conn.Close();
                    conn.Dispose();
                    _dbTransaction.Dispose();
                }
            }
        }

        /// <summary>
        /// 根据实体对象删除数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public int? Delete<T>(string id)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    return conn.Delete<T>(id);
                }
                catch (Exception e)
                {
                    DbLog.WriteException(e);
                    return 0;
                }
            }
        }

        /// <summary>
        /// 带事务的实体对象更新数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public int? DeleteTran<T>(string id)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    _dbTransaction = conn.BeginTransaction();
                    var num = conn.Delete<T>(id, _dbTransaction);
                    _dbTransaction.Commit();
                    return num;
                }
                catch (Exception e)
                {
                    _dbTransaction.Rollback();
                    DbLog.WriteException(e);
                    return 0;
                }
                finally
                {
                    conn.Close();
                    conn.Dispose();
                    _dbTransaction.Dispose();
                }
            }
        }

        /// <summary>
        /// 根据实体对象删除数据
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public int? Delete(object entity)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    return conn.Delete(entity);
                }
                catch (Exception e)
                {
                    DbLog.WriteException(e);
                    return 0;
                }
            }
        }

        /// <summary>
        /// 带事务的实体对象更新数据
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public int? DeleteTran(object entity)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    _dbTransaction = conn.BeginTransaction();
                    var num = conn.Delete(entity, _dbTransaction);
                    _dbTransaction.Commit();
                    return num;
                }
                catch (Exception e)
                {
                    _dbTransaction.Rollback();
                    DbLog.WriteException(e);
                    return 0;
                }
                finally
                {
                    conn.Close();
                    conn.Dispose();
                    _dbTransaction.Dispose();
                }
            }
        }

        /// <summary>
        /// 根据实体对象删除数据列表
        /// </summary>
        /// <param name="param">参数</param>
        /// <returns></returns>
        public int? DeleteList<T>(object param)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    return conn.DeleteList<T>(param);
                }
                catch (Exception e)
                {
                    DbLog.WriteException(e);
                    return 0;
                }
            }
        }

        /// <summary>
        /// 带事务的实体对象删除数据列表
        /// </summary>
        /// <param name="param">参数</param>
        /// <returns></returns>
        public int? DeleteListTran<T>(object param)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    _dbTransaction = conn.BeginTransaction();
                    var num = conn.DeleteList<T>(param, _dbTransaction);
                    _dbTransaction.Commit();
                    return num;
                }
                catch (Exception e)
                {
                    _dbTransaction.Rollback();
                    DbLog.WriteException(e);
                    return 0;
                }
                finally
                {
                    conn.Close();
                    conn.Dispose();
                    _dbTransaction.Dispose();
                }
            }
        }

        /// <summary>
        /// 根据实体对象删除数据列表
        /// </summary>
        /// <param name="strWhere">Where条件</param>
        /// <returns></returns>
        public int? DeleteList<T>(string strWhere)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    return conn.DeleteList<T>(strWhere);
                }
                catch (Exception e)
                {
                    DbLog.WriteException(e);
                    return 0;
                }
            }
        }

        /// <summary>
        /// 带事务的实体对象删除数据列表
        /// </summary>
        /// <param name="strWhere">Where条件</param>
        /// <returns></returns>
        public int? DeleteListTran<T>(string strWhere)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    _dbTransaction = conn.BeginTransaction();
                    var num = conn.DeleteList<T>(strWhere, _dbTransaction);
                    _dbTransaction.Commit();
                    return num;
                }
                catch (Exception e)
                {
                    _dbTransaction.Rollback();
                    DbLog.WriteException(e);
                    return 0;
                }
                finally
                {
                    conn.Close();
                    conn.Dispose();
                    _dbTransaction.Dispose();
                }
            }
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        /// <param name="strWhere">Where条件</param>
        /// <returns></returns>
        public int RecordCount<T>(string strWhere)
        {
            SetDialect();
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    return conn.RecordCount<T>(strWhere);
                }
                catch (Exception e)
                {
                    DbLog.WriteException(e);
                    return 0;
                }

            }
        }

        #endregion

        #region 根据 SQL 返回影响行数

        /// <summary>
        /// 根据SQL返回影响行数
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns></returns>
        public object GetObjectValue(StringBuilder sql)
        {
            return this.GetObjectValue(sql, null);
        }
        /// <summary>
        /// 根据SQL返回影响行数,带参数
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="param">参数化</param>
        /// <returns></returns>
        public object GetObjectValue(StringBuilder sql, object param)
        {
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    return conn.ExecuteScalar(sql.ToString(), param);
                }
                catch (Exception e)
                {

                    DbLog.WriteException(e);
                    return null;
                }

            }

        }
        #endregion

        #region 根据 SQL 执行
        /// <summary>
        ///  根据SQL执行
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns>object</returns>
        public int ExecuteBySql(StringBuilder sql)
        {
            return this.ExecuteBySql(sql, null);
        }
        /// <summary>
        ///  根据SQL执行,带参数
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="param">参数化</param>
        /// <returns>object</returns>
        public int ExecuteBySql(StringBuilder sql, object param)
        {
            int num = 0;
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    _dbTransaction = conn.BeginTransaction();
                    num = conn.Execute(sql.ToString(), param, _dbTransaction);
                    _dbTransaction.Commit();
                }
                catch (Exception e)
                {

                    _dbTransaction.Rollback();
                    num = -1;
                    DbLog.WriteException(e);
                }
                finally
                {
                    conn.Close();
                    conn.Dispose();
                    _dbTransaction.Dispose();
                }

            }
            return num;
        }

        /// <summary>
        ///  根据SQL执行,带参数,不带事务
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="param">参数化</param>
        /// <returns>object</returns>
        public int ExecuteBySqlNotTran(StringBuilder sql, object param)
        {
            int num = 0;
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    num = conn.Execute(sql.ToString(), param);

                }
                catch (Exception e)
                {
                    num = -1;
                    DbLog.WriteException(e);
                }

            }
            return num;
        }
        /// <summary>
        /// 批量执行SQL语句
        /// </summary>
        /// <param name="sqls">sql语句</param>
        /// <param name="param">参数化</param>
        /// <returns></returns>
        public int BatchExecuteBySql(object[] sqls, object[] param)
        {
            int num = 0;
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    _dbTransaction = conn.BeginTransaction();
                    for (int i = 0; i < sqls.Length; i++)
                    {
                        StringBuilder builder = (StringBuilder)sqls[i];
                        if (builder != null)
                        {
                            object paramArray = param[i];
                            conn.Execute(builder.ToString(), paramArray, _dbTransaction);
                        }
                    }
                    _dbTransaction.Commit();
                    num = 1;
                }
                catch (Exception e)
                {
                    num = -1;
                    _dbTransaction.Rollback();
                    DbLog.WriteException(e);
                }
                finally
                {
                    conn.Close();
                    conn.Dispose();
                    _dbTransaction.Dispose();
                }
            }
            return num;
        }
        #endregion

        #region 根据 SQL 返回 DataTable 数据集
        /// <summary>
        /// 根据 SQL 返回 DataTable 数据集
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns>DataTable</returns>
        public DataTable GetDataTableBySqL(StringBuilder sql)
        {
            return this.GetDataTableBySqL(sql, null);
        }
        /// <summary>
        /// 根据 SQL 返回 DataTable 数据集，带参数
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="param">参数化</param>
        /// <returns>DataTable</returns>
        public DataTable GetDataTableBySqL(StringBuilder sql, object param)
        {
            using (IDbConnection conn = OpenConnection())
            {
                try
                {
                    return DbReader.ReaderToDataTable(conn.ExecuteReader(sql.ToString(), param));
                }
                catch (Exception e)
                {
                    DbLog.WriteException(e);
                    return null;
                }
            }

        }
        #endregion

        #region 根据 SQL 返回 IList
        /// <summary>
        /// 根据 SQL 返回 IList
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="sql">语句</param>
        /// <returns></returns>
        public List<T> GetDataListBySqL<T>(StringBuilder sql)
        {
            return this.GetDataListBySqL<T>(sql, null);
        }
        /// <summary>
        /// 根据 SQL 返回 IList,带参数 (比DataSet效率高)
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="sql">Sql语句</param>
        /// <param name="param">参数化</param>
        /// <returns></returns>
        public List<T> GetDataListBySqL<T>(StringBuilder sql, object param)
        {
            using (IDbConnection conn = OpenConnection())
            {
                try
                {

                    return DbReader.ReaderToList<T>(conn.ExecuteReader(sql.ToString(), param));
                }
                catch (Exception e)
                {
                    DbLog.WriteException(e);
                    return null;
                }
            }


        }
        #endregion

        #region 数据分页 返回 DataTable
        /// <summary>
        /// 数据分页
        /// </summary>
        /// <param name="sql">传入要执行sql语句</param>
        /// <param name="param">参数</param>
        /// <param name="orderby">排序字段和排序类型</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="count">返回查询条数</param>
        /// <returns>返回Table类型</returns>
        public DataTable GetPageTable(string sql, object param, string orderby, int pageIndex, int pageSize, ref int count)
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                if (DbType == SqlSourceType.SqlServer)
                {
                    int num = (pageIndex - 1) * pageSize;
                    int num1 = pageIndex * pageSize;
                    sb.Append("Select * From (Select ROW_NUMBER() Over (Order By " + orderby + " ");
                    sb.Append(") As rowNum, * From (" + sql + ") As T ) As N Where rowNum > " + num + " And rowNum <= " + num1 + "");
                    count = Convert.ToInt32(this.GetObjectValue(new StringBuilder("Select Count(1) From (" + sql + ") As t"), param));
                }
                else if (DbType == SqlSourceType.SqLite)
                {
                    //当前从第几条记录开始查询数据,判断是否是第一页
                    int num = (pageIndex - 1) * pageSize;
                    sb.Append("Select * From (" + sql + ") As T Order By " + orderby + "  limit " + pageSize + " offset  " + num + " ");
                    count = Convert.ToInt32(this.GetObjectValue(new StringBuilder("Select Count(1) From (" + sql + ") As t"), param));
                }
                return this.GetDataTableBySqL(sb, param);
            }
            catch (Exception e)
            {
                DbLog.WriteException(e);
                return null; ;
            }
        }

        /// <summary>
        /// 数据分页
        /// </summary>
        /// <param name="sql">传入要执行sql语句</param>
        /// <param name="orderby">排序字段和排序类型</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="count">返回查询条数</param>
        /// <returns>返回Table类型</returns>
        public DataTable GetPageTable(string sql, string orderby, int pageIndex, int pageSize, ref int count)
        {
            return GetPageTable(sql, null, orderby, pageIndex, pageSize, ref count);
        }
        #endregion

        #region 数据分页 返回 ILis
        /// <summary>
        /// 数据分页
        /// </summary>
        /// <param name="sql">传入要执行sql语句</param>
        /// <param name="param">参数</param>
        /// <param name="orderby">排序字段和排序类型</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="count">返回查询条数</param>
        /// <returns>返回List类型</returns>
        public IList GetPageList<T>(string sql, object param, string orderby, int pageIndex, int pageSize, ref int count)
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                if (DbType == SqlSourceType.SqlServer)
                {
                    int num = (pageIndex - 1) * pageSize;
                    int num1 = pageIndex * pageSize;
                    sb.Append("Select * From (Select ROW_NUMBER() Over (Order By " + orderby + " ");
                    sb.Append(") As rowNum, * From (" + sql + ") As T ) As N Where rowNum > " + num + " And rowNum <= " + num1 + "");
                    count = Convert.ToInt32(this.GetObjectValue(new StringBuilder("Select Count(1) From (" + sql + ") As t"), param));
                }
                else if (DbType == SqlSourceType.SqLite || DbType == SqlSourceType.MySql)
                {
                    //当前从第几条记录开始查询数据,判断是否是第一页
                    int num = (pageIndex - 1) * pageSize;
                    sb.Append("Select * From (" + sql + ") As T Order By " + orderby + "  limit " + pageSize + " offset  " + num + " ");
                    count = Convert.ToInt32(this.GetObjectValue(new StringBuilder("Select Count(1) From (" + sql + ") As t"), param));
                }

                return this.GetDataListBySqL<T>(sb, param);
            }
            catch (Exception e)
            {
                DbLog.WriteException(e);
                return null; ;
            }
        }

        /// <summary>
        /// 数据分页
        /// </summary>
        /// <param name="sql">传入要执行sql语句</param>
        /// <param name="orderby">排序字段和排序类型</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="count">返回查询条数</param>
        /// <returns>返回List类型</returns>
        public IList GetPageList<T>(string sql, string orderby, int pageIndex, int pageSize, ref int count)
        {
            return GetPageList<T>(sql, null, orderby, pageIndex, pageSize, ref count);
        }
        #endregion

        #region IDisposable 成员
        /// <summary>
        /// 内存回收
        /// </summary>
        public void Dispose()
        {

            if (_dbTransaction != null)
            {
                this._dbTransaction.Dispose();
            }
        }
        #endregion
    }
}
